package main

import (
	"bufio"
	"fmt"
	"os"
	"regexp"
	"strconv"
	"strings"
)

type NPUInfo struct {
	Name           string
	Health         string
	Power          float64
	Temp           int
	HugepagesUsage string
	AICoreUsage    int
	MemoryUsage    string
}

func main4() {
	filePath := "/Users/gaozizhong/code/go_learning/gpu_demo/demo2.txt"
	file, err := os.Open(filePath)
	if err != nil {
		fmt.Printf("无法打开文件: %v\n", err)
		return
	}
	defer file.Close()

	var npuInfos []NPUInfo
	scanner := bufio.NewScanner(file)
	// 跳过标题行
	for i := 0; i < 5; i++ {
		scanner.Scan()
	}

	re := regexp.MustCompile(`^\| *(\d+) +(\d+) *\| *(\w+) *\| *([\d.]+) * +(\d+) * +(\d+ */ *\d+) *\| *(\d+) * +(\d+ */ *\d+) *\|$`)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "+") {
			continue
		}
		matches := re.FindStringSubmatch(line)
		if len(matches) == 9 {
			power, _ := strconv.ParseFloat(matches[4], 64)
			temp, _ := strconv.Atoi(matches[5])
			aiCoreUsage, _ := strconv.Atoi(matches[7])

			npuInfo := NPUInfo{
				Name:           matches[2],
				Health:         matches[3],
				Power:          power,
				Temp:           temp,
				HugepagesUsage: matches[6],
				AICoreUsage:    aiCoreUsage,
				MemoryUsage:    matches[8],
			}
			npuInfos = append(npuInfos, npuInfo)
		}
	}

	if err := scanner.Err(); err != nil {
		fmt.Printf("读取文件时出错: %v\n", err)
		return
	}

	// 输出解析结果
	for _, info := range npuInfos {
		fmt.Printf("名称: %s, 健康状态: %s, 功率: %.1f W, 温度: %d C, 大页使用情况: %s, AICore使用率: %d%%, 内存使用情况: %s\n",
			info.Name, info.Health, info.Power, info.Temp, info.HugepagesUsage, info.AICoreUsage, info.MemoryUsage)
	}
}
